#pragma once
#ifndef LUA_H
#define LUA_H

struct lua_State;

typedef int(*LuaFunction)(lua_State*);

class LuaState
{
public:

	LuaState() : mState(0), mOwn(false) {}
	LuaState(lua_State* state) : mState(state), mOwn(false) {}
	~LuaState() { Destroy(); }

	void Create();
	void Destroy();

	void LoadFile(const char* filename);

	void SaveState(const char* filename);

	void GetGlobal(const char* name);
	void SetGlobal(const char* name);

	void SetField(int index, const char* key);
	void GetField(int index, const char* key);

	void NewTable();
	int GetLength(int n);

	void StartIteration();
	bool Next();

	void Pop(int count = 1);

	enum
	{
		OK = 0,
		Yielded = 1,
	};

	void DoCall(int args);
	int DoCallYieldable(int args);

	int AsInteger(int n);
	const char* AsString(int n);

	void PushInt(int v);
	void PushString(const char* v);
	void PushFunction(LuaFunction func);

	int CheckLuaError(int res);

private:

	lua_State* mState;
	bool mOwn;

};

#endif